#!/usr/bin/python3


import os

class _Store():
    store={}
    changed=False

    def __init__(self,*file):
        self.file=os.path.join(*file)
        self.init()

    def init(self):
        self.loadLine()

    def loadLine(self):
        # if _get_file_size(self.file)<=0:
        #     return
        if not os.path.exists(self.file):
            return
        with open(self.file,"r") as f:
            while True:
                line=f.readline()
                if not line:
                    # line 都是 "\n结尾或者末行==文字"
                    break
                line=line.strip(" \n;")
                x=line.split("===")
                # if len(x)<2:
                #     x=line.split("=")
                if len(x)<2:
                    continue
                self.store[x[0]]="===".join(x[1:])
        self.changed=False

    def has(self,key):
        if self.store.get(key) is None:
            return False
        return True
    
    def get(self,key):
        return self.store.get(key)

    def add(self,k,v):
        self.store[k]=v
        self.changed=True

    def delete(self,key):
        # == pop
        value=self.has(key)
        if value is None:
            return None
        
        del self.store[key]
        return value

    def save(self):
        print(" ?save: ",self.file)
        if not self.changed:
            return
        with open(self.file,"w") as f:
            print("+save: ",self.file)
            # save ids...
            for k in self.store.keys():
                f.write("{}==={}\n".format(k,self.store.get(k)))
        self.changed=False


class GlobalState:
    dataStore=[]
    doneStore={}

    f_bvid_to_aid="_bvid_to_aid.txt"
    f_aid_to_cid="_aid_to_cid.txt"
    f_bangumi_to_sid="_bangumi_to_seasonid.txt"
    f_follow301_url="_follow301_url.txt"

    f_error="_errors.txt"

    def __init__(self,dir):
        self.aidStore:_Store=_Store(dir,self.f_bvid_to_aid)
        self.aid2cidStore:_Store=_Store(dir,self.f_aid_to_cid)
        self.seasonidStore:_Store=_Store(dir,self.f_bangumi_to_sid)
        self.follow301Store:_Store=_Store(dir,self.f_follow301_url)
        self.errorStore:_Store=_Store(dir,self.f_error)

    def isDone(self,url_or_id:str):
        # 存的是 aid, 各种id
        if self.doneStore.get(url_or_id) is None:
            return False
        return True
    
    def addDone(self,*args):
        for arg in args:
            self.doneStore[arg]=True
    
    def isError(self,url_or_id:str):
        # 存的是 aid, 各种id
        if self.errorStore.get(url_or_id) is None:
            return False
        return True
    
    def setError(self,*args):
        for arg in args:
            self.errorStore.add(arg,"x")
    
    ## bvid->aid->cid
    # getAid_byBvid
    def setAid(self,bvid:str,aid:str):
        self.aidStore.add(bvid,aid)
    def getAid(self,bvid:str):
        return self.aidStore.get(bvid)

    # setCid_byAid
    def setCid(self,aid,cid):
        self.aid2cidStore.add(aid,cid)
    def getCid(self,aid):
        return self.aid2cidStore.get(aid)
    
    def setSeasonid(self,key,seasonid):
        self.seasonidStore.add(key,seasonid)
    def getSeasonid(self,key):
        return self.seasonidStore.get(key)
    
    def setFollow301(self,url1,url2):
        self.follow301Store.add(url1,url2)

    def getFollow301(self,url1):
        return self.follow301Store.get(url1)

    def save(self):
        # save 各种id
        self.aidStore.save()
        self.aid2cidStore.save()
        self.seasonidStore.save()
        self.follow301Store.save()
        self.errorStore.save()


